package org.langqiao.network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

import javax.swing.text.AbstractDocument.BranchElement;

public class ServerThread implements Runnable{

	Socket s = null;
	
	BufferedReader br = null;
	
	public ServerThread(Socket s) throws IOException{
		this.s = s;
		br = new BufferedReader(new InputStreamReader(s.getInputStream()));
	}
	
	@Override
	public void run() {
		try{
			String content = null;
			if((content = readFromClient())!=null){
				for(Socket s : MyServer.socketList){
					PrintStream ps = new PrintStream(s.getOutputStream());
					ps.print(content);
				}
			}
		}catch(Exception e){
			
		}
	}
	
	private String readFromClient() throws IOException{
		try {
			return br.readLine();
		} catch (Exception e) {
			//如果捕捉住异常，说明该socket对应的客服端已经关闭
			MyServer.socketList.remove(s);
		}
		return null;
	}
}
